<?php
$PageSecurity = 3;
include_once ('includes/session.inc');

$pageSize = 20;
$action = isset ( $_POST ['action'] ) ? $_POST ['action'] : 'get';

if ($action == 'get') {
    $total = 0;
    $limit = isset ( $_POST ['limit'] ) ? $_POST ['limit'] : $pageSize;
    $start = isset ( $_POST ['start'] ) ? $_POST ['start'] : 0;

    $conds = array ();
    getConditions ( $conds, 'locked', '=', '0' );
    getConditions ( $conds, 'album_number', 'like', $_POST ['srch_album_number'] );
    getConditions ( $conds, 'album_name', 'like', $_POST ['srch_album_name'] );
    getConditions ( $conds, 'album_author', 'like', $_POST ['srch_album_author'] );

    $result = _single ( 'api/album' )->getAlbumPaginates ( $limit, $start, $total, $conds );

    $data = '{"success":true';
    $data .= ',"data":[';
    $haveData = false;

    while ( $row = DB_fetch_array ( $result ) ) {
        $haveData = true;
        $data .= sprintf ( '{"album_id":"%s","album_number":"%s","album_name":"%s","album_author":"%s",
            "created_date":"%s"},', $row ['album_id'], $row ['album_number'], $row ['album_name'], $row ['album_author'], $row ['created_date'] );
    }

    if ($haveData) {
        $data = substr ( $data, 0, strlen ( $data ) - 1 );
    }

    $data .= '],"count":"' . json_encode ( $total ) . '"}';
    echo $data;
} elseif ($action == 'insert') {
    // Get datas:
    $data = array ();
    if (! empty ( $_POST ['album_number'] ))
        $data ['album_number'] = DB_escape_string ( trim ( $_POST ['album_number'] ) );
    if (! empty ( $_POST ['album_name'] ))
        $data ['album_name'] = DB_escape_string ( trim ( $_POST ['album_name'] ) );
    if (! empty ( $_POST ['album_author'] ))
        $data ['album_author'] = DB_escape_string ( trim ( $_POST ['album_author'] ) );

    $result = array ();
    $checkNumberExisted = _single ( 'api/album' )->verifyFieldExist ( 'album_number', $data ['album_number'] );
    $result ['error'] = 0;
    if ($checkNumberExisted) {
        $result ['error'] = 1;
    } else {
        $result ['id'] = _single ( 'api/album' )->insert ( $data, $_SESSION ['UserID'], $_SESSION ['Password'] );
    }
    ;
    echo '{"success":true,"data":' . json_encode ( $result ) . '}';
} elseif ($action == 'update') {
    $data = array ();
    if (! empty ( $_POST ['field'] ))
        $data ['field'] = $_POST ['field'];
    if (! empty ( $_POST ['value'] ))
        $data ['value'] = DB_escape_string ( trim ( $_POST ['value'] ) );
    if (! empty ( $_POST ['album_id'] ))
        $data ['album_id'] = $_POST ['album_id'];
        // Execute
    $result = array ();
    $result ['error'] = 0;
    if ($_POST ['field'] == 'album_number') {
        $checkNumberExisted = _single ( 'api/album' )->verifyFieldExist ( 'album_number', $data ['value'] );
        if ($checkNumberExisted) {
            $result ['error'] = 1;
        } else {
            $result ['data'] = _single ( 'api/album' )->update ( $data, $_SESSION ['UserID'], $_SESSION ['Password'] );
        }
    } else {
        $result ['data'] = _single ( 'api/album' )->update ( $data, $_SESSION ['UserID'], $_SESSION ['Password'] );
    }
    // Response
    echo '{"success":true ,"data":' . json_encode ( $result ) . '}';
} elseif ($action == 'delete') {
    $data = array ();
    $delete_rs = _single ( 'api/album' )->delete ( $_POST ['album_id'], $_SESSION ['UserID'], $_SESSION ['Password'] );
    if ($delete_rs === false) {
        echo '{"success":false ,"data":' . json_encode ( null ) . '}';
    } else {
        echo '{"success":true ,"data":' . json_encode ( $_POST ['album_id'] ) . '}';
    }
}
function getConditions(&$conditions, $field, $relation, $value) {
    if (isset ( $value ) && (! empty ( $value ) || $field == 'locked')) {
        $value = ($relation == 'like') ? "N'%" . DB_escape_string ( trim ( $value ) ) . "%'" : DB_escape_string ( trim ( $value ) );
        array_push ( $conditions, array (
                'field' => $field,
                'relation' => $relation,
                'value' => $value
        ) );
    }
    ;
}
;
?>
